import Vue from 'vue'
import {throttle} from "./index"

/**
 * action 权限名称  
 * effect 操作  disabled/delete
 */
let permissions = []
export const permissionFn = () => {
    import('@/store').then(store=>{
        permissions = store.default.state.userInfo.permissions || []
    })
}
permissionFn()
Vue.directive('permission', {
    inserted(el, binding) {
        const action = binding.value.action
        const effect = binding.value.effect
        if (permissions.indexOf(action) === -1) {
            if (effect === 'disabled') {
                el.disabled = true
                el.classList.add('is-disabled')
            } else {
                el.remove()
            }
        }
    }
})

let io
Vue.directive('loadMore', {
    bind(el, binding,vnode) {
        const _el = binding.value
        io = new IntersectionObserver((e)=>{
            if(e[0].isIntersecting){
                if(vnode.context.loadMoreLoading || vnode.context.finish) return
                _el()
            }
        },{root:el.querySelector(".el-scrollbar")})
        io.observe(el.querySelector(".observer_dom"))
    },
    unbind() {
        io.disconnect()
    }
})

